﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web.Mail;

namespace yeqw.FrameWork
{
  
    public class SendEmailInfo
    {
        public string SmtpLoginName { set; get; }
        public string SmtpLoginPwd { set; get; }
        public string SmtpServer { set; get; }
        public string Address { set; get; }
        public string DisplayName { set; get; }
        public string ToEmail { set; get; }
        public string Subject { set; get; }
        public string Body { set; get; }
        public bool IsBodyHtml { set; get; }
        public Encoding DisplayNameEncoding { set; get; }
        public string ServerFileName { set; get; }

        public ResultInfo<string> SendEmail()
        {

            ResultInfo<string> result = new ResultInfo<string>();
            System.Web.Mail.MailMessage m_Mail = new System.Web.Mail.MailMessage();
            //定义SMTP服务器的名称 
            SmtpMail.SmtpServer = "smtp.qq.com";

            //定义SMTP邮件服务器需要身份认证
            m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //认证的用户名
            m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "server@ourcs.net");
            //认证密码
            m_Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xingji1321");

            //定义邮件的发送地址 
            m_Mail.From = string.Format("\"{0}\"<{1}>",DisplayName,Address);
            
            //定义邮件的接收地址 
            m_Mail.To = ToEmail;

            //定义邮件的主题
            m_Mail.Subject = Subject;
            //定义邮件的主体 
            m_Mail.Body = Body;
            //'邮件以 HTML的格式发送 
            m_Mail.BodyFormat = MailFormat.Html;
            //定义邮件的有限级，在此设定为高
            m_Mail.Priority = System.Web.Mail.MailPriority.High;
           
            try
            {
                SmtpMail.Send(m_Mail);
                result.IsError = false;
                result.Description = "邮件发送成功。";
            }
            catch (Exception ex)
            {
                 result.IsError = true;
                result.Description = "邮件发送失败，失败原因：" + ex.Message;
            }
            return result;

        }


        #region NetMail 
        //public ResultInfo<string> SendEmail()
        //{

        //    ResultInfo<string> result = new ResultInfo<string>();
        //    //初始化一个发邮件发件人地址
        //    MailAddress from = new MailAddress(Address, DisplayName, DisplayNameEncoding);
        //    //发邮件用到协议
        //    SmtpClient client = new SmtpClient(SmtpServer);
        //    client.UseDefaultCredentials = false;

        //    //发邮件身份验证
        //    client.Credentials = new System.Net.NetworkCredential(SmtpLoginName, SmtpLoginPwd);
        //    //发邮件的处理方式为使用网络方式
        //    client.DeliveryMethod = SmtpDeliveryMethod.Network;
        //    //创建一个发邮件的对象
        //    MailMessage message = new MailMessage();
        //    message.From = from;
        //    string toemail = ToEmail;
        //    //收信人地址
        //    message.To.Add(toemail);

        //    //邮件主题
        //    message.Subject = Subject;
        //    //编码
        //    message.BodyEncoding = System.Text.Encoding.Default;
        //    //邮件正文
        //    message.Body = Body;
        //    //是否是HTML代码
        //    message.IsBodyHtml = IsBodyHtml;

        //    /******************附件部分******************/
        //    if (!string.IsNullOrEmpty(ServerFileName))
        //    {
        //        message.Attachments.Add(new Attachment(ServerFileName));
        //    }
        //    try
        //    {
        //        //发送
        //        client.Send(message);
        //        message.Dispose();
        //        result.IsError = false;
        //        result.Description = "邮件发送成功。";
        //    }
        //    catch (Exception ex)
        //    {
        //        message.Dispose();
        //        result.IsError = false;
        //        result.Description = "邮件发送失败，失败原因：" + ex.Message;
        //    }
        //    return result;

        //}
        #endregion
    }
}
